﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ClientRegister
{
    public partial class OldStudentRegisCourseForm : Form
    {

        public string id { set; get; }
        public string name { set; get; }
        public string birthday { set; get; }
        public string gender { set; get; }
        public string career { set; get; }
        public string email { set; get; }
        public string phone { set; get; }
        public string address { set; get; }

        string[] idClass;

        SynchronizeData sync = new SynchronizeData();

        public OldStudentRegisCourseForm()
        {
            InitializeComponent();
        }

        private void OldStudentRegisCourseForm_Load(object sender, EventArgs e)
        {

            ////Load du lieu tu server
            //sync.GetDataForInsertMethodWithID(sync.studentTb, sync.insertcol);
            //sync.GetDataForUpdateMethodWithID(sync.studentTb, sync.updatecol);

            ////Load du lieu lop hoc
            //sync.GetDataForInsertMethodWithNoneID(sync.ClassOfStu, sync.insertcol);
            //sync.GetDataForUpdateMethodWithNoneID(sync.ClassOfStu, sync.updatecol);

            //load thong tin hoc vien
            txtFullName.Text = name;
            txtBirthday.Text = birthday;
            txtGender.Text = gender;
            txtJob.Text = career;
            txtPhone.Text = phone;
            txtAddress.Text = address;
            txtEmail.Text = email;
            //load combobox khoa hoc
            CourseDataProvider course = new CourseDataProvider();
            DataSet dscourse = course.loadCourses();
            cbbCourse.DataSource = dscourse.Tables[0];
            cbbCourse.DisplayMember = "NameCourse";
            cbbCourse.ValueMember = "ID";
        }

        private void btnRegis_Click(object sender, EventArgs e)
        {

            ////Load du lieu tu server
            //sync.GetDataForInsertMethodWithID(sync.studentTb, sync.insertcol);
            //sync.GetDataForUpdateMethodWithID(sync.studentTb, sync.updatecol);

            ////Load du lieu lop hoc
            //sync.GetDataForInsertMethodWithNoneID(sync.ClassOfStu, sync.insertcol);
            //sync.GetDataForUpdateMethodWithNoneID(sync.ClassOfStu, sync.updatecol);

             StudentInClassDataProvider stdClass = new StudentInClassDataProvider();
            int result = 0;
            //string xmlStuClass = "";

            for (int i = 0; i < idClass.Length; i++)
            {
                result = stdClass.InsertStudentInClass(id, idClass[i]);
                if (result == 0) break;
                //xmlStuClass += stdClass.selectClassOfStu(id, idClass[i]);
            }

            ////Chuyen du lieu len server
            //sync.transfDataForInsertMethod(xmlStuClass, sync.ClassOfStu);
            if (result != 0)
                MessageBox.Show("Đăng ký thành công");
            else MessageBox.Show("Đăng ký không thành công");
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

        }

        private void button_Clear_Click(object sender, EventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbbCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Display ngay khai giang
            if (cbbCourse.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                CourseDataProvider courseinfo = new CourseDataProvider();
                SqlDataReader sdrCourse = courseinfo.loadCourseInfo(cbbCourse.SelectedValue.ToString());
                if (sdrCourse.Read())
                    txtKG.Text = DateTime.Parse(sdrCourse.GetValue(2).ToString()).ToShortDateString();
                sdrCourse.Close();
            }
            //Display thong tin lop hoc ra DataGridView
            if (cbbCourse.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                ClassDataProvider clsList = new ClassDataProvider();
                DataSet dsClass = clsList.loadClass(cbbCourse.SelectedValue.ToString());
                DataTable dtClass = dsClass.Tables[0];
                GridListClass.DataSource = dtClass;

                idClass = new string[dtClass.Rows.Count];

                for (int i = 0; i < dtClass.Rows.Count; i++)
                {
                    idClass[i] = dtClass.Rows[i].ItemArray[0].ToString();
                }
                //Tinh tong hoc phi
                int totalFee = clsList.loadTotalFee(cbbCourse.SelectedValue.ToString());
                txtTotalFee.Text = totalFee.ToString();
            }
        }
    }
}
